package com.example.qingyiapp.ui.activity.fragment.fragment_FrientUI.childfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.qingyiapp.service.entify.IntrodutioBean;
import com.example.qingyiapp.R;
import com.example.qingyiapp.service.entify.Databean;
import com.example.qingyiapp.service.entify.FindFriend;
import com.example.qingyiapp.service.entify.FindTJBean;
import com.example.qingyiapp.service.entify.FriendSquareBean;
import com.example.qingyiapp.service.entify.FriendXxBean;
import com.example.qingyiapp.service.presenter.PersenterMannaer;
import com.example.qingyiapp.service.view.IChildView;
import com.example.qingyiapp.ui.activity.Friend_xr_item_activity;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv2;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_lv3;
import com.example.qingyiapp.ui.activity.fragment.fragment_AddressUI.address_bean.JavaBean_msg;
import com.example.qingyiapp.ui.activity.fragment.fragment_FrientUI.Adapter.friend_fragment_square_adapter;
import com.example.qingyiapp.ui.activity.fragment.fragment_FrientUI.Entify.Squareentify;
import com.example.qingyiapp.ui.activity.fragment.fragment_FrientUI.Interface.IOnClickListener;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author ：created by 申德丰
 * Create Date ：2019/9/17 14:12
 * Package_Name : QingYiApp
 */
public class Frient_child_fragment extends Fragment {


    @BindView(R.id.square_xrecyclerview)
    XRecyclerView squareXrecyclerview;
    Unbinder unbinder;

    ArrayList<Squareentify> squareentifies=new ArrayList<>();

    final IChildView iChildView = new IChildView() {
        @Override
        public void onSuccess(Databean s) {

        }

        @Override
        public void onError(String error) {

        }

        @Override
        public void ongetAddress_lvData(JavaBean_lv javaBean_lv) {

        }

        @Override
        public void ongetAddress_lvData2(JavaBean_lv2 javaBean_lv2) {

        }

        @Override
        public void ongetAddress_lvData3(JavaBean_lv3 javaBean_lv3) {

        }

        @Override
        public void ongetAddress_lvData4(JavaBean_msg javaBean_lv4) {

        }

        @Override
        public void ongetfriendSquareBean(FriendSquareBean friendSquareBean) {

            Log.d("########", friendSquareBean.toString());
            List<FriendSquareBean.DataBean> data = friendSquareBean.getData();


            FriendSquareBean.DataBean dataBean = data.get(0);

            String postsrc = dataBean.getPostsrc();

            Glide.with(getContext()).load(postsrc).into(imagehera);
            textView.setText(dataBean.getTitle());
            for (int i = 1; i < data.size(); i++) {
                FriendSquareBean.DataBean dataBean1 = data.get(i);
                String company_id = dataBean1.getCompany_id();
                String postcover1 = dataBean1.getPostcover();
                String detail = dataBean1.getDetail();
                String start_date = dataBean1.getStart_date();
                String end_date = dataBean1.getEnd_date();
                String time = start_date + "至" + end_date;
                List<?> attach_imgs = dataBean1.getAttach_imgs();
                FriendSquareBean.DataBean.UserBean user = dataBean1.getUser();
                String name = user.getName();
                String raw_img = user.getRaw_img();
                List<FriendSquareBean.DataBean.ScenicsBean> scenics = dataBean1.getScenics();
                FriendSquareBean.DataBean.ScenicsBean scenicsBean = scenics.get(0);
                String sname = scenicsBean.getSname();

                Squareentify squareentify = new Squareentify(raw_img, name, sname, time, detail, postcover1, attach_imgs,company_id);
                squareentifies.add(squareentify);
            }
            friend_fragment_square_adapter.notifyDataSetChanged();


        }

        @Override
        public void ongetfindfriend(FindFriend findFriend) {

        }

        @Override
        public void getjsonstring(FindTJBean s) {

        }

        @Override
        public void getfriendxxbean(FriendXxBean friendXxBean) {

        }

        @Override
        public void getIntrodution1(IntrodutioBean introdutioBean) {

        }
    };
    private ImageView imagehera;
    private TextView textView;
    private PersenterMannaer persenterMannaer;

    private int page=0;
    private friend_fragment_square_adapter friend_fragment_square_adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        squareentifies.clear();
        final View inflate = inflater.inflate(R.layout.fragment_child_friend_square_ui, container, false);

        unbinder = ButterKnife.bind(this, inflate);

        persenterMannaer=new PersenterMannaer(getContext(),"http://www.iqingyi.com/");
        persenterMannaer.onCreate();
        persenterMannaer.attchview(iChildView);

        View   view=View.inflate(getContext(),R.layout.fragment_chlid_square_headview,null);
         imagehera = view.findViewById(R.id.image_head);
         textView = view.findViewById(R.id.texthead);

        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        squareXrecyclerview.setLayoutManager(linearLayoutManager);
        squareXrecyclerview.addHeaderView(view);
        persenterMannaer.getfriendsquare(page,20);
        friend_fragment_square_adapter = new friend_fragment_square_adapter(squareentifies, getContext());
        squareXrecyclerview.setAdapter(friend_fragment_square_adapter);
        squareXrecyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=0;
                persenterMannaer.getfriendsquare(page,20);
                squareXrecyclerview.refreshComplete();

            }

            @Override
            public void onLoadMore() {
                page+=20;
                persenterMannaer.getfriendsquare(page,20);
                squareXrecyclerview.loadMoreComplete();
//                friend_fragment_square_adapter.notifyDataSetChanged();
            }
        });

        friend_fragment_square_adapter.setiOnClickListener(new IOnClickListener() {
            @Override
            public void onxreviewitemonclick(int company_id) {
                Intent intent=new Intent(getContext(),Friend_xr_item_activity.class);
                intent.putExtra("uid",squareentifies.get(company_id).getCompany_id());
                startActivity(intent);
            }
        });

        return inflate;

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
